﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

using Xc.DataAccess.Core;
using Xc.DataAccess.DataBase.DeviceInfos.Models;
using System.Data;
using Xc.DataAccess.DataBase.DeviceInfos.Extensions;

namespace Xc.DataAccess.DataBase.DeviceInfos
{
    /// <summary>
    /// 设备报警服务
    /// </summary>
    /// <typeparam name="TDbContext"></typeparam>
    public class DbDeviceInfoAlarmService<TDbContext> : TableInfoService<TDbContext, AlarmInfo, AlarmInfoHistory>, IDbDeviceInfoAlarmService where TDbContext : DbContext, IDbDevice
    {
        /// <summary>
        /// 设备报警服务
        /// </summary>
        /// <param name="dbContextFactory"></param>
        public DbDeviceInfoAlarmService(IDbContextFactory<TDbContext> dbContextFactory) : base(dbContextFactory)
        {

        }
        /// <summary>
        /// 获取报警信息
        /// </summary>
        /// <param name="deviceid"></param>
        /// <returns></returns>
        public override ResultData<AlarmInfo[]> Gets(string deviceid)
        {
            using var db = _dbContextFactory.CreateDbContext();
            var properties = db.AlarmInfos.Where(p => p.DeviceInfoId == deviceid);
            return ResultData<AlarmInfo[]>.Accept("获取成功", properties.ToArray());
        }
        /// <summary>
        /// 获取报警信息历史
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="dateTimeStart"></param>
        /// <param name="dateTimeEnd"></param>
        /// <returns></returns>
        public override Task<AlarmInfoHistory[]> Gets(string deviceId, DateTime? dateTimeStart, DateTime? dateTimeEnd)
        {
            using var db = _dbContextFactory.CreateDbContext();
            var properties = db.AlarmInfoHistorys.Where(p => p.DeviceInfoId == deviceId).Between(dateTimeStart, dateTimeEnd);
            return properties.ToArrayAsync();
        }
        /// <summary>
        /// 删除指定报警信息
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public override async Task<Result> Remove(string deviceId, string name)
        {
            using var db = _dbContextFactory.CreateDbContext();
            var alarms=await db.AlarmInfos.Where(p => p.DeviceInfoId == deviceId && p.Name == name).ToArrayAsync();
            db.AlarmInfos.RemoveRange(alarms);
            return Result.From(db.SaveChanges(),s=>s>0);
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public override Task<Result> Set(Dictionary<string, UnitString> valuePairs, string deviceId)
        {
            return Task.Run(() =>
            {
                var dt = DateTime.Now;
                using var db = _dbContextFactory.CreateDbContext();
                return this.SetValue(db, db.AlarmInfos, db.AlarmInfoHistorys, valuePairs, deviceId);
            });
        }
    }

}
